﻿using _064.App;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace _064
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public string strsql = "server=.;database=Mydatabase;user=sa;pwd=35683568";//创建公共字段，数据库连接字符串
        public SqlConnection conn;//初始化对象
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 insert = new Form2();
            insert.Owner = this;
            insert.Show();
        }
        public void bindData(List<SqlData> list)
        {
            //构建表结构
            listView1.Columns.Add("编号");
            listView1.Columns.Add("用户名");
            listView1.Columns.Add("密码");
            //将集合中的数据放入listView1中
            foreach (SqlData data in list)
            {
                ListViewItem it = new ListViewItem();//构建一个listviewitem对象,通过它把数据放入listview1中
                it.Text = data.Code;
                it.SubItems.Add(data.Name);
                it.SubItems.Add(data.Pwd);
                listView1.Items.Add(it);//将数据放入listview1的items集合中

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bindData(new SqlDA().select());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            bindData(new SqlDA().select(textBox1.Text));
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)//查询数据库表
        {
            try
            {
                conn = new SqlConnection(strsql);//实例化对象
                conn.Open();//打开数据库
                SqlCommand cmd = new SqlCommand();//创建对象
                cmd.Connection = conn;
                cmd.CommandText = "select count(*) from " + textuser.Text.Trim();
                cmd.CommandType = CommandType.Text;
                int i = Convert.ToInt32(cmd.ExecuteScalar());
                lbdata.Text = "数据库表中共有" + i.ToString() + "条数据";
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form3 insert = new Form3();
            insert.Owner = this;
            insert.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //将输入的内容转换成SqlData格式
            SqlData data = new SqlData();
            //调用修改数据函数
            new SqlDA().delete(data);
        }
    }
}
